package com.heima.tess4j;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.Tesseract1;
import net.sourceforge.tess4j.TesseractException;

import java.io.File;
import java.net.URL;

/**
 * @author 谢金成
 * @date 2024/4/11 14:07
 */
public class Application {

    /**
     * 识别图片中的文字
     * @param args
     */
    public static void main(String[] args) throws TesseractException {

        // 创建实例
        ITesseract tesseract = new Tesseract();

        // 设置字体库路径
        URL systemResource = ClassLoader.getSystemResource("");
        tesseract.setDatapath("D:\\java\\heima\\heima-leadnews\\heima-leadnews-test\\tess4j-demo\\src\\main\\resources\\tessdata");

        // 设置语言
        tesseract.setLanguage("chi_sim");

        // 识别图片
        File file = new File(systemResource.getPath() + "img/image-20210524161243572.png");
        String result = tesseract.doOCR(file);
        System.out.println(result.replaceAll("\\r|\\n", "-"));
    }
}
